# coding=utf-8
import xlrd, xlwt
import openpyxl

class ExcelData():

    def __init__(self, data_path, save_path, ole_value, new_value, save_sheet):
        self.data_path = data_path
        self.save_path = save_path
        self.data = openpyxl.load_workbook(self.data_path)
        self.save_sheet = save_sheet
        self.sheet_name = self.data.sheetnames
        self.table = self.data[self.sheet_name[0]]
        self.rowNum = self.table.max_row
        self.colNum = self.table.max_column
        self.write = openpyxl.Workbook()
        self.write_sheel = self.write.create_sheet(self.save_sheet, index=0)
        self.ole_value = ole_value
        self.new_value = new_value

    def read_excel(self):
        datas = []
        for i in range(1, self.rowNum + 1):
            sheet_data = []
            for j in range(1, self.colNum + 1):
                c_cell = self.table.cell(i, j).value
                sheet_data.append(c_cell)
            datas.append(sheet_data)

        return datas

    def write_excel(self):
        data = self.read_excel()
        rows = 0
        for row in range(len(data)):
            rows += 1
            cols = 0
            for col in range(len(data[row])):
                if col == 0 and row != 0:
                    value = data[row][col].replace(self.ole_value, self.new_value)
                else:
                    value = data[row][col]
                cols += 1
                self.write_sheel.cell(rows, cols).value = value

        self.write.save(self.save_path)
        print("执行结束")


if __name__ == "__main__":

    path = "C:\\Users\\Administrator\\Desktop\\Untitled33.xlsx"
    path2 = "C:\\Users\\Administrator\\Desktop\\Untitled44.xlsx"
    data = ExcelData(path,path2,"1629","2042","sheet0").write_excel()
    print(data)